package com.lineage.data.item_etcitem.extra;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.ExtraMagicWeaponTable;
import com.lineage.server.datatables.lock.CharWeaponTimeReading;
import com.lineage.server.datatables.sql.CharWeaponTimeTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ItemStatus;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1MagicWeapon;
import java.sql.Timestamp;
import java.util.Random;

public class MagicWeapon extends ItemExecutor {
  private static final Random _random = new Random();
  
  public static ItemExecutor get() {
    return new MagicWeapon();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int targObjId = data[0];
    L1ItemInstance tgItem = pc.getInventory().getItem(targObjId);
    if (tgItem == null)
      return; 
    if (tgItem.getItem().getType2() != 1) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aD注意！只能对武器附加，并且再次强化可覆盖或更换其他附魔技能。"));
      return;
    } 
    int steps = CharWeaponTimeTable.get_steps(targObjId);
    if (steps >= 0)
      steps++; 
    if (steps == -1)
      steps = 0; 
    if (tgItem.get_magic_weapon() != null && tgItem.get_magic_weapon().getItemId() != item.getItemId())
      steps = 0; 
    L1MagicWeapon magicWeapon = ExtraMagicWeaponTable.getInstance().get(item.getItemId(), steps);
    if (magicWeapon == null && steps >= 0) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aD请注意！已达该属性最高阶级。"));
      return;
    } 
    if (magicWeapon == null && steps == -1)
      return; 
    pc.getInventory().removeItem(item, 1L);
    if (_random.nextInt(100) >= magicWeapon.getSuccessRandom()) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage(magicWeapon.getFailureMsg()));
      return;
    } 
    boolean update = false;
    update = (tgItem.get_magic_weapon() != null);
    tgItem.set_magic_weapon(magicWeapon);
    if (magicWeapon.getMaxUseTime() > 0) {
      Timestamp ts = new Timestamp(System.currentTimeMillis() + (magicWeapon.getMaxUseTime() * 1000));
      tgItem.set_time(ts);
    } else {
      tgItem.set_time(null);
    } 
    if (update) {
      if (tgItem.get_magic_weapon().getItemId() == item.getItemId()) {
        CharWeaponTimeReading.get().updateTime(tgItem.getId(), tgItem.get_time(), 
            tgItem.get_magic_weapon().getItemId(), steps, 1);
      } else {
        CharWeaponTimeReading.get().updateTime(tgItem.getId(), tgItem.get_time(), 
            tgItem.get_magic_weapon().getItemId(), steps, 0);
      } 
    } else {
      CharWeaponTimeReading.get().addTime(tgItem.getId(), tgItem.get_time(), 
          tgItem.get_magic_weapon().getItemId());
    } 
    pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
    pc.sendPackets((ServerBasePacket)new S_SystemMessage(magicWeapon.getSuccessMsg()));
  }
}
